/**
 * Mock service for testing when API is not available
 * This file provides mock implementations of API services that return Promise objects
 * to simulate API calls
 */

// 车辆数据模拟 - 简化为只包含t_car表字段
const carData = [
  {
    id: 1,
    carNum: 'BJ2023001',
    brand: '奔驰',
    guidePrice: 50.98,
    produceTime: '2023-01-10',
    carType: '燃油车'
  },
  {
    id: 2,
    carNum: 'BJ2023002',
    brand: '宝马',
    guidePrice: 52.39,
    produceTime: '2023-01-15',
    carType: '燃油车'
  },
  {
    id: 3,
    carNum: 'BJ2023003',
    brand: '奥迪',
    guidePrice: 45.80,
    produceTime: '2022-05-15',
    carType: '燃油车'
  },
  {
    id: 4,
    carNum: 'BJ2023004',
    brand: '特斯拉',
    guidePrice: 28.99,
    produceTime: '2022-12-10',
    carType: '电动车'
  },
  {
    id: 5,
    carNum: 'BJ2023005',
    brand: '丰田',
    guidePrice: 22.58,
    produceTime: '2022-04-10',
    carType: '混动车'
  }
];

/**
 * 分页查询模拟
 * @param {Object} data 查询参数
 * @returns {Promise} 返回异步操作的Promise对象
 */
function mockPagination(data, source) {
  const { pageNum = 1, pageSize = 10 } = data;
  
  // 筛选数据
  let filteredData = [...source];
  
  // 如果有筛选条件，应用筛选
  if (data) {
    Object.keys(data).forEach(key => {
      if (data[key] && key !== 'pageNum' && key !== 'pageSize') {
        filteredData = filteredData.filter(item => 
          item[key] && item[key].toString().toLowerCase().includes(data[key].toString().toLowerCase())
        );
      }
    });
  }
  
  // 分页
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  const records = filteredData.slice(start, end);
  
  return {
    code: 200,
    message: 'success',
    data: {
      rows: records,
      total: filteredData.length
    }
  };
}

/**
 * 查询车辆列表
 */
export function mockListCar(params) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(mockPagination(params, carData));
    }, 300);
  });
}

/**
 * 获取车辆详情
 */
export function mockGetCar(id) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const car = carData.find(item => item.id.toString() === id.toString());
      if (car) {
        resolve({
          code: 200,
          message: 'success',
          data: car
        });
      } else {
        reject({
          code: 404,
          message: '车辆不存在'
        });
      }
    }, 300);
  });
}

/**
 * 添加车辆
 */
export function mockAddCar(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const newId = Math.max(...carData.map(item => item.id)) + 1;
      const newCar = { ...data, id: newId };
      carData.push(newCar);
      resolve({
        code: 200,
        message: '添加成功',
        data: newCar
      });
    }, 300);
  });
}

/**
 * 更新车辆
 */
export function mockUpdateCar(data) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const index = carData.findIndex(item => item.id === data.id);
      if (index !== -1) {
        carData[index] = { ...carData[index], ...data };
        resolve({
          code: 200,
          message: '更新成功',
          data: carData[index]
        });
      } else {
        reject({
          code: 404,
          message: '车辆不存在'
        });
      }
    }, 300);
  });
}

/**
 * 删除车辆
 */
export function mockDeleteCar(id) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const ids = id.split(',').map(Number);
      const initialLength = carData.length;
      
      // 过滤掉要删除的车辆
      const newCarData = carData.filter(item => !ids.includes(item.id));
      
      if (newCarData.length < initialLength) {
        // 替换原数组内容
        carData.length = 0;
        carData.push(...newCarData);
        
        resolve({
          code: 200,
          message: '删除成功',
          data: null
        });
      } else {
        reject({
          code: 404,
          message: '车辆不存在'
        });
      }
    }, 300);
  });
}

/**
 * 批量删除车辆
 */
export function mockBatchDeleteCar(ids) {
  return mockDeleteCar(ids);
}

/**
 * 导出车辆数据
 */
export function mockExportCar() {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 模拟二进制数据
      const blob = new Blob(['模拟的Excel文件内容'], { type: 'application/vnd.ms-excel' });
      resolve(blob);
    }, 300);
  });
} 